<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <template id="t1">
      <div>
        <p @input="changeValue">
          爱好
          <input
            type="checkbox"
            :checked="form.hobbys.includes('css')"
            value="css"
          />
          CSS
          <input
            type="checkbox"
            :checked="form.hobbys.includes('html')"
            value="html"
          />
          HTML
          <input
            type="checkbox"
            :checked="form.hobbys.includes('js')"
            value="JS"
          />
          JS {{form.hobbys}}
        </p>
      </div>
    </template>
    <script src="./vue.js"></script>
    <script>
      const vm = new Vue({
        el: "#app",
        template: "#t1",
        data: {
          form: {
            hobbys: ["css", "html"],
          },
        },
        methods: {
          changeValue(e) {
            const value = e.target.value;
            const checked = e.target.checked;
            if (checked) {
              this.form.hobbys.push(value);
            } else {
              const index = this.form.hobbys.indexOf(value);
              this.form.hobbys.splice(index, 1);
            }
          },
        },
      });
    </script>
  </body>
</html>
